import 'package:flutter/material.dart';
import 'package:wishdom_mall/pages/cart/index.dart';
import 'package:wishdom_mall/pages/cateory/index.dart';
import 'package:wishdom_mall/pages/home/index.dart';
import 'package:wishdom_mall/pages/my/index.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 记录当前选中的索引
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: currentIndex, // 当前选中哪个页面
          children: const [HomePage(), CategoryPage(), CartPage(), MyPage()],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: barItemList(),
        currentIndex: currentIndex,
        selectedItemColor: Colors.red,
        unselectedItemColor: Colors.black,
        onTap: (index) {
          // 更新状态
          setState(() {
            currentIndex = index; 
          });
        },
      ),
    );
  }

  /// 返回一个数组, 数组中放一个个的BarItem
  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> list = [];
    list.add(BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/home.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/home-active.png',
          width: 32,
          height: 32,
        ),
        label: '首页'));
    list.add(BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/cate.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/cate-active.png',
          width: 32,
          height: 32,
        ),
        label: '分类'));
    list.add(BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/cart.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/cart-active.png',
          width: 32,
          height: 32,
        ),
        label: '购物车'));
    list.add(BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/user.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/user-active.png',
          width: 32,
          height: 32,
        ),
        label: '我的'));
    return list;
  }
}
